#!/usr/bin/env python
"""
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: jack_connect.py
Date: July 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

Manage the Jack connections

"""

#-----------------------------------------------------------------------
# System imports
import logging
import sys
import os
from time import sleep
import jack
import Pyro.core
import Pyro.naming

# Application imports
import config_service as config
import logger_service as logger

#-----------------------------------------------------------------------
class JackConnect():
    
    #---------------------------------------------------------------------------
    def __init__(self):
        if issubclass(JackConnect, Pyro.core.ObjBase):
            Pyro.core.ObjBase.__init__(self)
        
        # Get us a logger
        self.logger = logging.getLogger('pylink.logger')
        
    def connect(self):        
        
        self.logger.info('Connecting jack devices...')
        
        #-------------------------------------------------------------------
        # Init jack and make our connections
        mode = config.mode()
        if mode == 'standalone':
            # We are a standalone machine and connect straight to the audio device
            # We connect through the DSP
            # We pull samples off the main soundcard, through the dsp and out to the main souncard
            jack.attach("pydsp")
            jack.register_port("in_1", jack.IsInput)
            jack.register_port("in_2", jack.IsInput)
            jack.register_port("out_1", jack.IsOutput)
            jack.register_port("out_2", jack.IsOutput)
            jack.activate()
            self.logger.info("Connecting standalone devices")
            jack.connect(config.indev_ch1(), "pydsp:in_1")
            jack.connect(config.indev_ch2(), "pydsp:in_2")
            jack.connect("pydsp:out_1", config.outdev_ch1())
            jack.connect("pydsp:out_2", config.outdev_ch2())
        elif mode == 'master':
            # We are the master machine in a netjack network and talk to the primary soundcard
            # We only run a network daemon at this end
            # We pull samples off the main soundcard and push them to the network
            # We pull samples off the network and push them to the main soundard
            jack.attach("pydaemon")
            jack.activate()
            self.logger.info("Connecting master devices")
            jack.connect(config.indev_ch1(), config.outnet_ch1())
            jack.connect(config.indev_ch2(), config.outnet_ch2())            
        else:
            # We are the slave machine in a netjack network
            # We pull samples off the network, through the dsp and out to this machines souncard
            jack.attach("pydsp")
            jack.register_port("in_1", jack.IsInput)
            jack.register_port("in_2", jack.IsInput)
            jack.register_port("out_1", jack.IsOutput)
            jack.register_port("out_2", jack.IsOutput)
            jack.activate()
            self.logger.info("Connecting slave devices")
            jack.connect(config.inslave_ch1(), "pydsp:in_1")
            jack.connect(config.inslave_ch2(), "pydsp:in_2")
            jack.connect("pydsp:out_1", config.outalsa_ch1())
            jack.connect("pydsp:out_2", config.outalsa_ch2())
    
    #-------------------------------------------------------------------
    def disconnect(self):
        # Disconnect and deactivate jack
        mode = config.mode()
        if mode == 'standalone':
            self.logger.info("Disconnecting standalone devices")
            jack.disconnect(config.indev_ch1(), "pydsp:in_1")
            jack.disconnect(config.indev_ch2(), "pydsp:in_2")
            jack.disconnect("pydsp:out_1", config.outdev_ch1())
            jack.disconnect("pydsp:out_2", config.outdev_ch2())
        elif mode == 'master':
            self.logger.info("Disconnecting master devices")
            jack.disconnect(config.indev_ch1(), config.outnet_ch1())
            jack.disconnect(config.indev_ch2(), config.outnet_ch2())
        else:
            self.logger.info("Disconnecting slave devices")
            jack.disconnect(config.inslave_ch1(), "pydsp:in_1")
            jack.disconnect(config.inslave_ch2(), "pydsp:in_2")
            jack.disconnect("pydsp:out_1", config.outalsa_ch1())
            jack.disconnect("pydsp:out_2", config.outalsa_ch2())
        jack.deactivate()
        jack.detach()